package cn.luyou.admin.service.impl;

import cn.luyou.admin.entity.TrainingPaperQuestion;
import cn.luyou.admin.mapper.TrainingPaperQuestionMapper;
import cn.luyou.admin.service.TrainingPaperQuestionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TrainingPaperQuestionServiceImpl extends ServiceImpl<TrainingPaperQuestionMapper, TrainingPaperQuestion> implements TrainingPaperQuestionService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(Long paperId, List<TrainingPaperQuestion> questions) {
        // 先删除原有题目
        deleteByPaperId(paperId);
        // 保存新题目
        questions.forEach(q -> q.setPaperId(paperId));
        saveBatch(questions);
    }

    @Override
    public List<TrainingPaperQuestion> getQuestionsByPaperId(Long paperId) {
        return list(new LambdaQueryWrapper<TrainingPaperQuestion>()
                .eq(TrainingPaperQuestion::getPaperId, paperId)
                .orderByAsc(TrainingPaperQuestion::getQuestionOrder));
    }

    @Override
    public void deleteByPaperId(Long paperId) {
        remove(new LambdaQueryWrapper<TrainingPaperQuestion>()
                .eq(TrainingPaperQuestion::getPaperId, paperId));
    }
} 